class DeadLockThread implements Runnable{
	static Object Chopsticks = new Object();
	static Object KnifeAndFork = new Object();
	private boolean flag;
	DeadLockThread(boolean flag){
		this.flag = flag;
	}

	public void run(){
		if(flag){
		while(true){
			synchronized(Chopsticks){
				System.out.println(Thread.currentThread().getName()+"---if---Chopsticks");
				}
			}
		}
	}else{
		while(true){
			synchronized(KnifeAndFork){
				System.out.println(Thread.currentThread().getName()+"---else---KnifeAndFork");
				synchronized(Chopsticks){
				System.out.println(Thread.currentThread().getName()+"---else---Chopsticks");	
					}
				}
			}
		}
	}
}

public class Example14{
	public static void main(String[] args){
		DeadLockThread d1 = new DeadLockThread(true);
		DeadLockThread d2 = new DeadLockThread(false);
		
		new Thread(d1,"Chinese").start();
		new Thread(d2,"America").start();
	}
}